package com.sergio.compress.utils;

import com.google.protobuf.Timestamp;
import com.sergio.compress.model.base.MockDataBase;
import com.sergio.compress.model.base.MockDataWrapper;
import com.sergio.compress.model.base.TestDataBase;
import com.sergio.compress.model.base.TestDataChildBase;
import com.sergio.compress.model.proto.Gift.TestData;
import com.sergio.compress.model.proto.Gift;
import com.sergio.compress.model.proto.Mock;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : Sergio.D
 * @Description :
 * @CreateDate ：2023/6/15 19:15
 */
public class TransUtil {

    public static Mock.MockDataWrapper convertToProto(MockDataWrapper mockDataWrapper) {
        Mock.MockDataWrapper.Builder wrapperBuilder = Mock.MockDataWrapper.newBuilder();

        List<Mock.MockDataBase> protoDataList = new ArrayList<>();
        for (MockDataBase mockDataBase : mockDataWrapper.getTestDataBaseList()) {

            protoDataList.add(convertToProto(mockDataBase));
        }

        wrapperBuilder.addAllTestDataBaseList(protoDataList);

        return wrapperBuilder.build();
    }

    public static Mock.MockDataBase convertToProto(MockDataBase mockDataBase) {
        Mock.MockDataBase.Builder dataBuilder = Mock.MockDataBase.newBuilder();

        dataBuilder.setStr8(mockDataBase.getStr8());
        dataBuilder.setStr16(mockDataBase.getStr16());
        dataBuilder.setStr32(mockDataBase.getStr32());
        dataBuilder.setStr64(mockDataBase.getStr64());
        dataBuilder.setStr256(mockDataBase.getStr256());
        dataBuilder.setGoodsId(mockDataBase.getGoodsId());
        dataBuilder.setPresentAt(convertToTimestamp(mockDataBase.getPresentAt()));
        dataBuilder.setAa(com.google.protobuf.ByteString.copyFrom(mockDataBase.getAa()));
        dataBuilder.setBb(mockDataBase.getBb());
        dataBuilder.setCc(mockDataBase.getCc());
        dataBuilder.setEe(mockDataBase.getEe());

        if (mockDataBase.getChildBase() != null) {
            Mock.MockDataChildBase.Builder childBuilder = Mock.MockDataChildBase.newBuilder();

            childBuilder.setUserId(mockDataBase.getChildBase().getUserId());
            childBuilder.setUserName(mockDataBase.getChildBase().getUserName());
            childBuilder.setUserId1(mockDataBase.getChildBase().getUserId1());
            childBuilder.setUserName1(mockDataBase.getChildBase().getUserName1());
            childBuilder.setUserId2(mockDataBase.getChildBase().getUserId2());
            childBuilder.setUserName2(mockDataBase.getChildBase().getUserName2());
            childBuilder.setAa(mockDataBase.getChildBase().getAa());
            childBuilder.setBb(mockDataBase.getChildBase().getBb());
            childBuilder.setAa1(mockDataBase.getChildBase().getAa1());
            childBuilder.setBb2(mockDataBase.getChildBase().getBb2());

            dataBuilder.setChildBase(childBuilder.build());
        }
        return dataBuilder.build();
    }

    private static Timestamp convertToTimestamp(Instant instant) {
        long seconds = instant.getEpochSecond();
        int nanos = instant.getNano();
        return Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build();
    }

    public static Gift.TestData convertToProto(TestDataBase testData) {
        TestData.Builder testDataBuilder = TestData.newBuilder();

        testDataBuilder.setPrepareId(testData.getPrepareId());
        testDataBuilder.setRoomId(testData.getRoomId());
        testDataBuilder.addAllReceiverUserIds(testData.getReceiverUserIds());
        testDataBuilder.setGoodsId(testData.getGoodsId());
        testDataBuilder.setGoodsQuantity(testData.getGoodsQuantity());
        testDataBuilder.setName(testData.getName());
        testDataBuilder.setChannelId(testData.getChannelId());
        testDataBuilder.setPresenterUserId(testData.getPresenterUserId());

        Gift.TestDataChild.Builder childBuilder;
        for (TestDataChildBase child : testData.getChild()) {
            childBuilder = Gift.TestDataChild.newBuilder();
            childBuilder.setName(child.getName());
            childBuilder.setUserId(child.getUserId());
            testDataBuilder.addChild(childBuilder.build());
        }

        Timestamp presentAtTimestamp = Timestamp.newBuilder()
                .setSeconds(testData.getPresentAt().getTime() / 1000)
                .build();
        testDataBuilder.setPresentAt(presentAtTimestamp);

        testDataBuilder.setAcquireFortuneQuantity(testData.getAcquireFortuneQuantity());
        testDataBuilder.setAcquirePopQuantity(testData.getAcquirePopQuantity());

        return testDataBuilder.build();
    }

    public static List<Gift.TestData> convertToProto(List<TestDataBase> testDataList) {
        List<Gift.TestData> protoList = new ArrayList<>();
        for (TestDataBase testData : testDataList) {
            protoList.add(convertToProto(testData));
        }
        return protoList;
    }


}
